package cn.melon.mod.library;

import java.util.List;

import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import cn.melon.Helper.LoggerHelper;
import cn.melon.base.BaseController;
import cn.melon.mod.login.UserInfo;

public class LibraryController extends BaseController{
    
    public String TAG = LoggerHelper.makeLogTag(LibraryController.class);
    public Logger libraryLogger = LoggerHelper.serverLogger();
    private JSONObject put;

    public LibraryController(String request) {
        super(request);
    }

    @Override
    public void onResponse() {
        
        String req = getRequest();
        
        libraryLogger.debug(TAG + req); 
        try {
            JSONObject reqJSON = new JSONObject(req);
            String action = reqJSON.getString("action");

            switch (action) {
                case "ASKBORROW":
                    setResponse(askBorrow());
                    break;
                case "ASKHOT":
                    setResponse(askHot());
                    break;
                case "UPDATEBOOK":
                    LibraryBooksInfo info =         
                                    LibraryBooksInfo.toInfo(
                                                reqJSON.getJSONObject("content")
                                    );  // 获取客户端传来的书籍信息
                    setResponse(updateInfo(info));
                    break;
                case "ADDBOOK":
                    LibraryBooksInfo info_add =         
                                    LibraryBooksInfo.toInfo(
                                                reqJSON.getJSONObject("content")
                                    );  // 获取客户端传来的书籍信息
                    setResponse(addBook(info_add));
                    break;
                case "ALLBOOKS":
                    setResponse(allBooks());
                    break;
                    
                case "RENEWBOOK": 
                    String bookID = reqJSON.getString("content");
                    setResponse(reNewBook(bookID));
                    break;

                default:
                    setResponse("{\"code\": 300, \"content\":\"暂时无此功能\"}");
                    break;
            }
                
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    
    
    
    /**
     * 续借图书
     * @param bookID 书籍编号
     * @return String
     */
    private String reNewBook(String bookID) {

        JSONObject jRet = new JSONObject();

        ILibraryBooksDao booksDao = new LibraryBooksDaoImpl();

        LibraryBooksInfo info = new LibraryBooksInfo();
        info.setBookID(bookID);
        
        try {
            if(booksDao.renewBook(info, getUserInfo().getStudentNum())) {
                jRet.put("content", "续借成功");
                jRet.put("code", 200);
            } else {
                jRet.put("code", 300);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        
        return jRet.toString();
    }

    private String allBooks() {
        JSONObject jRet = new JSONObject();
        
        ILibraryBooksDao booksDao = new LibraryBooksDaoImpl();

        List<LibraryBooksInfo> infoList = booksDao.getAllBooks();
        
        try {
            if (infoList != null) {
                JSONArray jContent = new JSONArray();
                for (LibraryBooksInfo info : infoList) {
                    jContent.put(info.toJSON());
                }
                jRet.put("content", jContent);
                jRet.put("code", 200);
            } else {
                jRet.put("code", 300);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        
        return jRet.toString();
    }

    private String addBook(LibraryBooksInfo info) {
        UserInfo userInfo = getUserInfo();        
        JSONObject jRet = new JSONObject();
        ILibraryBooksDao booksDao = new LibraryBooksDaoImpl();

//        if (userInfo.getType().equals("student"))  { // 学生用户无此权限
//            try {
//                jRet.put("code", 300);
//                jRet.put("content", "该用户无此权限");
//            } catch (JSONException e) {
//                e.printStackTrace();
//            }
//            return jRet.toString();
//        }
//
        if(booksDao.addBook(info)) {
            try {
                jRet.put("code", 200);
                jRet.put("content", "添加信息成功");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            
        }
        return jRet.toString();
    }

    /**
     * 返回当前借书列表
     * @return String
     */
    public String askBorrow() {
        UserInfo userInfo = getUserInfo();
        JSONObject jRet = new JSONObject();
        
        ILibraryBooksDao booksDao = new LibraryBooksDaoImpl();

        List<LibraryBooksInfo> infoList = booksDao.getBorrowdBooks(userInfo.getStudentNum());
        
        try {
            if (infoList != null) {
                JSONArray jContent = new JSONArray();
                for (LibraryBooksInfo info : infoList) {
                    jContent.put(info.toJSON());
                }
                jRet.put("content", jContent);
                jRet.put("code", 200);
            } else {
                jRet.put("code", 300);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        
        return jRet.toString();
    }
    
    /**
     * 获取最近热门书籍
     * @return String
     */
    public String askHot() {
        UserInfo userInfo = getUserInfo();
        JSONObject jRet = new JSONObject();
        
        ILibraryBooksDao booksDao = new LibraryBooksDaoImpl();

        List<LibraryBooksInfo> infoList = booksDao.getHotBooks();
        
        try {
            if (infoList != null) {
                JSONArray jContent = new JSONArray();
                for (LibraryBooksInfo info : infoList) {
                    jContent.put(info.toJSON());
                }
                jRet.put("content", jContent);
                jRet.put("code", 200);
            } else {
                jRet.put("code", 300);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        
        return jRet.toString();
    }
    
    /**
     * 更新用户信息
     * @param booksInfo 书籍信息
     * @return String
     */
    public String updateInfo(LibraryBooksInfo booksInfo) {
        UserInfo userInfo = getUserInfo();
        JSONObject jRet = new JSONObject();

        ILibraryBooksDao booksDao = new LibraryBooksDaoImpl();
        
//        if (userInfo.getType().equals("student"))  { // 学生用户无此权限
//            try {
//                jRet.put("code", 300);
//                jRet.put("content", "该用户无此权限");
//            } catch (JSONException e) {
//                e.printStackTrace();
//            }
//            return jRet.toString();
//        }
        
        if (booksInfo == null) {
            try {
                jRet.put("code", 300);
                jRet.put("content", "客户端信息错误");
            } catch (JSONException e) {
                e.printStackTrace();
            }
            
            return jRet.toString();
        }
        
        if (booksDao.updateBooks(booksInfo)) {
            try {
                jRet.put("code", 200);
                jRet.put("content", "更新信息成功");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            try {
                jRet.put("code", 300);
                jRet.put("content", "更新信息失败");
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        
        return jRet.toString();
    }
}